home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jprefs.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  5.2 KB  |  175 lines

  1. # jprefs.tcl - utilities for user preferences and configuration
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5. ######################################################################
  6.  
  7. ### TO DO
  8.  
  9. ######################################################################
  10. # global variables:
  11. #
  12. global J_PREFS env
  13. if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
  14. if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
  15. #
  16. ######################################################################
  17.  
  18.  
  19. ######################################################################
  20. # j:source_config ?options? file - read user configuration from a file
  21. #   option is -directory
  22. # file is assumed to be in env(HOME)/.tk unless dir is specified
  23. # NOTE: this can also be used just to source an arbitrary Tcl file
  24. ######################################################################
  25.  
  26. proc j:source_config { args } {
  27.   j:parse_args { {directory {} } }
  28.  
  29.   set file [lindex $args 0]
  30.   global env
  31.  
  32.   if {$directory == {}} then {
  33.     set directory $env(HOME)/.tk
  34.   }
  35.  
  36.   if {[file isfile "$directory/$file"]} then {
  37.     uplevel 1 "source $directory/$file"
  38.   }
  39. }
  40.  
  41. ######################################################################
  42. # j:read_prefs ?options? defaults - read X defaults from file, set array
  43. # options are:
  44. #   -file (default defaults)
  45. #   -directory (default ~/.tk)
  46. #   -array (default J_PREFS)
  47. #   -prefix (default "")
  48. # <defaults> is a list of two-element sublists.  the first element of
  49. #   each sublist is the name of the default (in the file and in the
  50. #   $array array); the second is the value to use if no such default
  51. #   exists (ie, the hardwired application default)
  52. # If a _default_ is "tk_strictMotif", it sets the element of $array,
  53. #   but also the global tk_strictMotif variable
  54. # If -prefix is non-null, it (plus a comma) is prepended to each 
  55. #   preference name, so that for instance you can set -prefix to
  56. #   "friend" and access preferences (and array indices) like
  57. #   "friend,name", "friend,age", etc.
  58. ######################################################################
  59.  
  60. proc j:read_prefs { args } {
  61.   j:parse_args {
  62.     {array J_PREFS}
  63.     {prefix {}}
  64.     {directory {} }
  65.     {file defaults}
  66.   }
  67.   set defaults [lindex $args 0]
  68.   
  69.   global env tk_strictMotif $array
  70.   
  71.   if {"x$directory" == "x"} {
  72.     set directory $env(HOME)/.tk    ;# NOTE: created if necessary!
  73.   }
  74.   
  75.   if {"x$prefix" != "x"} {        ;# if prefix is non-null...
  76.     set prefix "$prefix,"        ;# ...add a comma to it
  77.   }
  78.   
  79.   set [format {%s(0)} $array] 1        ;# dummy to make sure it's an array
  80.  
  81.   catch {option readfile $directory/$file userDefault}
  82.  
  83.   foreach pair $defaults {
  84.     set pref_name [lindex $pair 0]
  85.     set hard_default [lindex $pair 1]
  86.     
  87.     set value [option get . $prefix$pref_name {}]
  88.     if {"x$value" == "x"} {set value $hard_default}
  89.     set [format {%s(%s)} $array $prefix$pref_name] $value
  90.     
  91.     if {"x$pref_name" == "xtk_strictMotif"} {
  92.       set tk_strictMotif $value
  93.     }
  94.   }
  95. }
  96.  
  97. ######################################################################
  98. # j:read_global_prefs - read common jstools preferences from ~/.tk/defaults
  99. ######################################################################
  100.  
  101. proc j:read_global_prefs {} {
  102.   global J_PREFS
  103.   
  104.   j:read_prefs {
  105.     {autoposition 0}
  106.     {bindings basic}
  107.     {typeover 1}
  108.     {confirm 1}
  109.     {visiblebell 1}
  110.     {audiblebell 1}
  111.     {printer lp}
  112.     {scrollbarside right}
  113.     {j_fs_fast 0}
  114.     {tk_strictMotif 0}
  115.   }
  116. }
  117.  
  118. # alias for backwards-compatibility:
  119. proc j:read_standard_prefs {} [info body j:read_global_prefs]
  120.  
  121. ######################################################################
  122. # j:write_prefs ?options? - write X defaults to file from array
  123. # options are:
  124. #   -file (default defaults)
  125. #   -directory (default ~/.tk)
  126. #   -array (default J_PREFS)
  127. #   -prefix (default "")
  128. # writes all elements of array $array
  129. # If -prefix is null, writes all elements of array $array which
  130. #   don't have a comma in their names.
  131. # If -prefix is non-null, writes all elements of array $array whose
  132. #   names start with "$prefix,"
  133. # For instance you can set -prefix to "friend" and access preferences
  134. #   (and array indices) like "friend,name", "friend,age", etc.
  135. ######################################################################
  136.  
  137. proc j:write_prefs { args } {
  138.   j:parse_args {
  139.     {array J_PREFS}
  140.     {prefix ""}
  141.     {directory {} }
  142.     {file defaults}
  143.   }
  144.   global env $array
  145.   
  146.   if {"x$directory" == "x"} then {
  147.     set directory $env(HOME)/.tk    ;# NOTE: created if necessary!
  148.   }
  149.  
  150.   if {! [file isdirectory $directory]} {;# make sure directory exists
  151.     exec mkdir $directory
  152.   }
  153.   set f [open $directory/$file {w}]
  154.   
  155.   if {"x$prefix" == "x"} {        ;# just names with no comma
  156.     foreach pref_name [lsort [array names $array]] {
  157.       if {[string first , $pref_name] == -1} {
  158.         set value [set [format {%s(%s)} $array $pref_name]]
  159.         puts $f "*${pref_name}:\t${value}"
  160.       }
  161.     }
  162.   } else {
  163.     foreach pref_name [lsort [array names $array]] {
  164.       if [string match "$prefix,*" $pref_name] {
  165.         set value [set [format {%s(%s)} $array $pref_name]]
  166.         puts $f "*${pref_name}:\t${value}"
  167.       }
  168.     }
  169.   }
  170.   
  171.   close $f
  172.   return 0
  173. }
  174.